// index.js
Page({
  data: {
    select: '0',
    search: 0,
    p3t: false,
    forumForm: null,
    forumSource: null,
    user: null,
    ad: null,
  },
  viewMyPermissions() {
    this.cShowModel('我的权限', '用户，可以畅快发言')
  },
  goMoveDetails(e) {
    wx.navigateTo({
      url: '/pages/forum-details/forum-details',
      fail: () => {
        wx.showToast({
          title: '页面加载失败',
        })
      }
    })
  },
  goMoveForum(e){
    let forumID=e.target.dataset.forumid
    wx.navigateTo({
      url: '/pages/forum/forum?forumID='+forumID,
    })
  },
  messageChange(e) {
    let i = e.currentTarget.dataset.index
    let v = this.data.p3t ? 1 : 0
    if (i == v) {
      return
    }
    this.setData({
      p3t: !this.data.p3t
    })
  },
  checkMyDetail() {
    if (this.data.user == null) {
      return false
    }
    return true
  },
  getForumForm() {
    let host = getApp().data.serverHost
    let token = wx.getStorageSync('token')
    wx.request({
      url: host + "/api/customize/get-index-form",
      method: "GET",
      header: {
        "Authorization": "Bearer " + token
      },
      success: (e) => {
        let response = e.data
        if (response.code == 200) {
          this.setData({
            forumForm: response.data,
            forumSource: response.data
          })
        } else {
          this.cShowModel('小程序请求', '网络出现错误')
        }
      },
      fail: (e) => {

      }
    })
  },
  checkTap(e) {
    let a = this.selectComponent('#tap-bar').data.select_d
    if (a != this.data.select) {
      this.setData({
        select: String(a)
      })
      if (a == '4') {
        let host = getApp().data.serverHost
        let userID = wx.getStorageSync('userID')
        let token = wx.getStorageSync('token')
        if (this.checkMyDetail()) {
          return
        }
        if (this.data.ad == null) {
          let host = getApp().data.serverHost
          let token = wx.getStorageSync('token')
          wx.request({
            url: host + '/api/ad/get',
            method: "GET",
            header: {
              Authorization: "Bearer " + token
            },
            success: (e) => {
              let response = e.data
              this.setData({
                ad: response.data
              })
            },
            fail: () => {

            }
          })
        }
        wx.request({
          url: host + "/api/my-detail/counter?userID=" + userID,
          method: "GET",
          header: {
            "Authorization": "Bearer " + token
          },
          success: (e) => {
            let response = e.data
            this.setData({
              user: response.data,
              "user.icon": wx.getStorageSync('icon'),
              "user.name": wx.getStorageSync('username')
            })
            this.setData({
              "user.lever": Math.floor(this.data.user.myEXP / 100)
            })
          },
          fail: (e) => {
            wx.showToast({
              title: '网络请求错误，请稍后再试',
              icon: "error"
            })
          }
        })
      }
    }
  },
  logout(){
    let host=getApp().data.serverHost
    let token=wx.getStorageSync('token')
    wx.request({
      url: host+'/api/auth/logout',
      header:{
        "Authorization":"Bearer "+token
      },
      method:"GET",
      success:(e)=>{
        let response=e.data
        if(response.code==200){
          wx.clearStorageSync()
          wx.redirectTo({
            url: '/pages/login/login',
          })
        }else{
          this.cShowModel('小程序提示','网络错误，退出登入失败')
        }
      }
    })
  },
  noDo() {
    this.cShowModel('小程序提示', '该功能暂未实现')
  },
  checkSearch(e) {
    let value=e.currentTarget.dataset.value
    let test=new RegExp(value)
    const index = e.currentTarget.dataset.index
    let arr=this.data.forumSource
    let use=[]
    if(this.data.search==index){
      return
    }
    if(value=='全部'){
      this.setData({
        forumForm:arr
      })
    }else{
      for(let i=0;i<arr.length;i++){
        if(test.test(arr[i].forumType)){
          use.push(arr[i])
        }
      }
      this.setData({
        forumForm:use
      })
    }
    this.setData({
      search: index
    })
  },
  goMoveSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
      fail: () => {
        wx.showToast({
          title: '页面加载失败',
          icon: 'error'
        })
      }
    })
  },
  goMoveReply(e) {
    let i = e.currentTarget.dataset.index
    let index = ["/pages/like/like", "/pages/reply/reply", "/pages/etmine/etmine", "/pages/fans/fans", "/pages/onsitemessages/onsitemessages"]
    wx.navigateTo({
      url: index[i],
      fail: () => {
        wx.showToast({
          title: '页面加载失败',
          icon: "error"
        })
      }
    })
  },
  myGoMove(e) {
    let i = e.currentTarget.dataset.index
    let index = ["/pages/my-post/my-post", "/pages/my-following/my-following", "/pages/my-collection/my-collection", "/pages/browsing-history/browsing-history"]
    wx.navigateTo({
      url: index[i],
      fail: () => {
        wx.showToast({
          title: '页面加载失败',
          icon: "error"
        })
      }
    })
  },
  cShowModel(title, content) {
    wx.showModal({
      title: title,
      content: content,
      showCancel: false
    })
  },
  onShow() {

  },
  onLoad(options) {
    let token = wx.getStorageSync('token')
    let host = getApp().data.serverHost
    let pass1 = true
    wx.request({
      url: host + "/api/test/hello",
      method: "GET",
      header: {
        "Authorization": "Bearer " + token
      },
      success: (e) => {
        let response = e.data
        if (response.code != 200) {
          wx.redirectTo({
            url: '/pages/login/login?verify=0',
          })
          pass1 = false
        }
      },
      fail: (e) => {
        wx.redirectTo({
          url: '/pages/login/login?verify=1',
        })
        pass1 = false
      }
    })
    if (!pass1) {
      return
    }
    let icon=wx.getStorageSync('icon')
    if (!icon) {
      let host = getApp().data.serverHost
      let userID = wx.getStorageSync('userID')
      let token = wx.getStorageSync('token')
      wx.request({
        url: host + "/api/my-detail/icon?userID=" + userID,
        method: "GET",
        header: {
          "Authorization": "Bearer " + token
        },
        success: (e) => {
          let response = e.data
          wx.setStorageSync('icon', response.data)
        },
        fail: (e) => {

        }
      })
    }
    this.getForumForm()
    let ofLogin = options.ofLogin
    if (ofLogin != null) {
      this.cShowModel('消息', '欢迎来到搞它搞它小程序,这里有许多秘密等你来发现')
    }
  }
})